package cn.iocoder.yudao.module.picturebook.controller.admin.bookshelf;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.picturebook.controller.admin.bookshelf.vo.*;
import cn.iocoder.yudao.module.picturebook.dal.dataobject.bookshelf.BookShelfDO;
import cn.iocoder.yudao.module.picturebook.service.bookshelf.BookShelfService;

@Tag(name = "绘本系统 - 用户书架关联")
@RestController
@RequestMapping("/picturebook/book-shelf")
@Validated
public class BookShelfController {

    @Resource
    private BookShelfService bookShelfService;

    @PostMapping("/create")
    @Operation(summary = "创建用户书架关联")
    @PreAuthorize("@ss.hasPermission('picturebook:book-shelf:create')")
    public CommonResult<Long> createBookShelf(@Valid @RequestBody BookShelfSaveReqVO createReqVO) {
        return success(bookShelfService.createBookShelf(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户书架关联")
    @PreAuthorize("@ss.hasPermission('picturebook:book-shelf:update')")
    public CommonResult<Boolean> updateBookShelf(@Valid @RequestBody BookShelfSaveReqVO updateReqVO) {
        bookShelfService.updateBookShelf(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户书架关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('picturebook:book-shelf:delete')")
    public CommonResult<Boolean> deleteBookShelf(@RequestParam("id") Long id) {
        bookShelfService.deleteBookShelf(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除用户书架关联")
                @PreAuthorize("@ss.hasPermission('picturebook:book-shelf:delete')")
    public CommonResult<Boolean> deleteBookShelfList(@RequestParam("ids") List<Long> ids) {
        bookShelfService.deleteBookShelfListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户书架关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('picturebook:book-shelf:query')")
    public CommonResult<BookShelfRespVO> getBookShelf(@RequestParam("id") Long id) {
        BookShelfDO bookShelf = bookShelfService.getBookShelf(id);
        return success(BeanUtils.toBean(bookShelf, BookShelfRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户书架关联分页")
    @PreAuthorize("@ss.hasPermission('picturebook:book-shelf:query')")
    public CommonResult<PageResult<BookShelfRespVO>> getBookShelfPage(@Valid BookShelfPageReqVO pageReqVO) {
        PageResult<BookShelfDO> pageResult = bookShelfService.getBookShelfPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BookShelfRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户书架关联 Excel")
    @PreAuthorize("@ss.hasPermission('picturebook:book-shelf:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBookShelfExcel(@Valid BookShelfPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BookShelfDO> list = bookShelfService.getBookShelfPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户书架关联.xls", "数据", BookShelfRespVO.class,
                        BeanUtils.toBean(list, BookShelfRespVO.class));
    }

}